<template>
  <div>
    <div class="ddosBox">
      <el-button type="success" size='small' @click="dialogVisible" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="success" size='small' @click="baseDialogVisible" class="config">{{$t('基本配置')}}</el-button>
    </div>
    <el-table ref="multipleTable" size='mini' :data="ddosList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="id" align='center' label="ID" width='80'>
        <template slot-scope="scope">
          {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column prop="name" align='center' :label="$t('策略名称')" width='150'>
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.name" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="addr" align='center' :label="$t('地址对象')" width='150'>
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.addr" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="iptype" align='center' :label="$t('IP类型')" :formatter="ipTypeShow" width='150'></el-table-column>
      <el-table-column prop="status" align='center' :label="$t('策略状态')" :formatter="statusShow" width='150'></el-table-column>
      <el-table-column prop="desc" align='center' :label="$t('描述')" width='350'>
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.desc" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column align='center' :width="lang === 'zh'?'420':'480'" fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
          <el-button type="primary" plain size="mini" @click="editCategoryDialogVisible(scope.row)" :class="'type' +(scope.$index+1)">{{$t('编辑防御类别')}}</el-button>
          <el-button type="success" plain size="mini" @click="checkBlackDialogVisible(scope.row)" :class="'find' +(scope.$index+1)">{{$t('查看黑名单')}}</el-button>
          <el-button type="danger" plain size="mini" @click="clearBlackList(scope.row)" :class="'clear' +(scope.$index+1)">{{$t('清空黑名单')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <ddos-dialog v-if="ddosDialogVisible" ref="refDdosDialogVisible"></ddos-dialog>
    <base-info-dialog v-if="baseInfoDialogVisible" ref="refBaseInfoDialogVisible"></base-info-dialog>
    <category-dialog v-if="categoryDialogVisible" ref="refCategoryDialogVisible"></category-dialog>
    <black-dialog v-if="blackDialogVisible" ref="refBlackDialogVisible"></black-dialog>
  </div>
</template>
<script>
import { getFloodArray, delFlood, cleanCategoryBlack } from '@/api/safe/ddos'
import DdosDialog from './components/DdosDialog'
import BaseInfoDialog from './components/BaseInfoDialog.vue'
import CategoryDialog from './components/CategoryDialog.vue'
import BlackDialog from './components/BlackDialog.vue'
export default {
  name: 'Ddos',
  components: {
    DdosDialog,
    BaseInfoDialog,
    CategoryDialog,
    BlackDialog
  },
  data () {
    return {
      ddosDialogVisible: false,
      blackDialogVisible: false,
      baseInfoDialogVisible: false,
      categoryDialogVisible: false,
      lang: this.$i18n.locale,
      ddosList: [],
      nameStr: ''
    }
  },
  created () {
    this.listData()
  },
  methods: {
    ipTypeShow (val) {
      return val.iptype === 'ipv4' ? 'IPv4' : 'IPv6'
    },
    statusShow (val) {
      return val.status === 'on' ? this.$t('开') : this.$t('关')
    },
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getFloodArray().then(res => {
        if (res.status === 0) {
          this.ddosList = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.ddosDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refDdosDialogVisible.init(data)
      })
    },
    // 基本配置
    baseDialogVisible () {
      this.baseInfoDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refBaseInfoDialogVisible.init()
      })
    },
    // 编辑防御类别
    editCategoryDialogVisible (data) {
      this.categoryDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refCategoryDialogVisible.init(data)
      })
    },
    // 查看黑名单
    checkBlackDialogVisible (data) {
      this.blackDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refBlackDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delFlood({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    clearBlackList (data) {
      this.$confirm(this.$t('您是否要清空已识别黑名单IP？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        cleanCategoryBlack({ name: data.name }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('清除成功'),
              type: 'success'
            })
          }
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
.ddosBox {
  margin-bottom: 10px;
}
</style>
